//
//  RHDeviceTopoME_61View.m
//  RuiHeApp
//
//  Created by Yan Chen on 2025/6/6.
//

#import "RHDeviceTopoME_61View.h"
#import "RHDeviceTopoBatteryItemView.h"
#define line_Width 1

@interface RHDeviceTopoME_61View()
@property (nonatomic, strong) QMUILabel *solarTitleLab;
@property (nonatomic, strong) QMUILabel *gridTitleLab;
@property (nonatomic, strong) QMUILabel *generatorTitleLab;
@property (nonatomic, strong) QMUILabel *loadTitleLab;

//光伏功率
@property (nonatomic, strong) QMUILabel *solarLab;
//光伏电压
@property (nonatomic, strong) QMUILabel *solarVolLab;
//电网功率
@property (nonatomic, strong) QMUILabel *gridLab;
//电网电压
@property (nonatomic, strong) QMUILabel *gridVolLab;
//发电机功率
@property (nonatomic, strong) QMUILabel *generatorLab;
//发电机电压
@property (nonatomic, strong) QMUILabel *generatorVolLab;
//电池功率
@property (nonatomic, strong) QMUILabel *batteryPowerLab;
//电池电压
@property (nonatomic, strong) QMUILabel *batteryVolLab;
//负载功率
@property (nonatomic, strong) QMUILabel *loadLab;
//负载电压
@property (nonatomic, strong) QMUILabel *loadVolLab;
//逆变器功率
@property (nonatomic, strong) QMUILabel *inverterLab;
//逆变器电压
@property (nonatomic, strong) QMUILabel *inverterVolLab;
//麦格瑞能并网负载功率
@property (nonatomic, strong) QMUILabel *parallelLab;

//soc
@property (nonatomic, strong) QMUILabel *socLab;
//soh
@property (nonatomic, strong) QMUILabel *sohLab;

@property (nonatomic, strong) UIImageView *solarImg;

@property (nonatomic, strong) UIImageView *inverterImg;

@property (nonatomic, strong) UIImageView *gridImg;

@property (nonatomic, strong) UIImageView *generatorImg;

@property (nonatomic, strong) UIImageView *batteryImg;

@property (nonatomic, strong) UIImageView *loadImg;
//麦格瑞能并网负载功率
@property (nonatomic, strong) UIImageView *parallelImg;
//光伏圆点
@property (nonatomic ,strong) UIView *solarDotView;
//电网圆点
@property (nonatomic ,strong) UIView *gridDotView;
//并网电网圆点
@property (nonatomic ,strong) UIView *parallel_gridDotView;
//负载圆点
@property (nonatomic ,strong) UIView *loadDotView;
//电池圆点
@property (nonatomic ,strong) UIView *batteryDotView;
//发电机圆点
@property (nonatomic ,strong) UIView *generatorDotView;

//光伏动画
@property (nonatomic ,strong) UIBezierPath *solarPath;
//负载动画
@property (nonatomic ,strong) UIBezierPath *loadPath;
//电网
@property (nonatomic ,strong) UIBezierPath *gridPath;

//电池动画
@property (nonatomic ,strong) UIBezierPath *batteryPath;
//发电机动画
@property (nonatomic ,strong) UIBezierPath *generationPath;


@property (nonatomic ,assign) BOOL isLandscape;


@property (nonatomic ,strong) NSArray *batViewArr;

@property (nonatomic ,strong) UIView *paralellView;

@property (nonatomic ,strong) UIView *itemContainView;

@property (nonatomic, assign) NSInteger scroll_index;

@property (nonatomic ,strong) UIScrollView *scrollView;

@end
@implementation RHDeviceTopoME_61View

-(instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        self.backgroundColor = [UIColor whiteColor];
     
        [self setupUI];
    }
    return self;
}

-(instancetype)initWithOrientation:(BOOL)isLandscape
{
    if(self = [super initWithFrame:CGRectZero]){
        self.backgroundColor = [UIColor whiteColor];
        self.isLandscape = isLandscape;
        [self setupUI];
    }
    return self;
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    [self setNeedsDisplay];
}


-(void)setupUI
{
    CGFloat valueFont = 12;
    kWeakSelf;
    UIColor *titleColor = kCOLOR_HEX_A(@"#171A1D", 0.6);
    //发电机
    UIImageView *generatorImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"kwt_home_topo_Generator"]];
    [self addSubview:generatorImg];
    self.generatorImg = generatorImg;
    [generatorImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(40);
        make.top.mas_offset(20);
        make.size.equalTo(@(CGSizeMake(32, 32)));
    }];
    
    QMUILabel *generatorTitleLab = [QMUILabel labelWithFont:kBOLDSYSTEMFONT(12) textColor:kCOLOR_HEX_A(@"#171A1D", 0.6)];
    generatorTitleLab.text = @"Generator";
    [self addSubview:generatorTitleLab];
    self.generatorTitleLab = generatorTitleLab;
    [generatorTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(generatorImg.mas_bottom).offset(5);
        make.centerX.equalTo(generatorImg);
    }];
    
    _generatorLab = [QMUILabel labelWithFont:kSYSTEMFONT(valueFont) textColor:kCOLOR_HEX(@"#3F4443")];
    _generatorLab.text = @"--kW";
    [self addSubview:_generatorLab];
    [_generatorLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(generatorImg.mas_right).offset(5);
        make.bottom.equalTo(generatorImg.mas_centerY).offset(-5);
    }];
    
    //负载
    UIImageView *loadImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"kwt_home_topo_load"]];
    [self addSubview:loadImg];
    self.loadImg = loadImg;
    [loadImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(generatorImg);
        make.centerY.equalTo(generatorImg);
        make.right.mas_offset(-40);
    }];
    
    QMUILabel *loadTitleLab = [QMUILabel labelWithFont:kBOLDSYSTEMFONT(12) textColor:kCOLOR_HEX_A(@"#171A1D", 0.6)];
    loadTitleLab.text = @"Load";
    [self addSubview:loadTitleLab];
    self.loadTitleLab = loadTitleLab;
    [loadTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(loadImg.mas_bottom).offset(5);
        make.centerX.equalTo(loadImg);
    }];
    
    _loadLab = [QMUILabel labelWithFont:kSYSTEMFONT(valueFont) textColor:kCOLOR_HEX(@"#3F4443")];
    _loadLab.text = @"--kW";
    [self addSubview:_loadLab];
    [_loadLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(loadImg.mas_centerY).offset(-5);
        make.right.equalTo(loadImg.mas_left).offset(-5);
    }];
 
    //光伏
    UIImageView *solarImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"kwt_home_topo_solar"]];
    [self addSubview:solarImg];
    self.solarImg = solarImg;
    [solarImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(generatorImg);
        make.left.equalTo(generatorImg);
        make.top.equalTo(weakSelf.generatorTitleLab.mas_bottom).offset(weakSelf.isLandscape?60:15);
    }];
    
    QMUILabel *solarTitleLab = [QMUILabel labelWithFont:kBOLDSYSTEMFONT(12) textColor:kCOLOR_HEX_A(@"#171A1D", 0.6)];
    solarTitleLab.text = @"Solar";
    self.solarTitleLab = solarTitleLab;
    [self addSubview:solarTitleLab];
    [solarTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(solarImg.mas_bottom).offset(5);
        make.centerX.equalTo(solarImg);
    }];
    
    _solarLab = [QMUILabel labelWithFont:kSYSTEMFONT(valueFont) textColor:kCOLOR_HEX(@"#3F4443")];
    _solarLab.text = @"--kW";
    [self addSubview:_solarLab];
    [_solarLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(solarImg.mas_right).offset(5);
        make.bottom.equalTo(solarImg.mas_centerY).offset(-5);
    }];
    
    //逆变器
    UIImageView *inverterImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"kwt_home_topo_inverter"]];
    [self addSubview:inverterImg];
    self.inverterImg = inverterImg;
    [inverterImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(solarImg);
        make.centerX.mas_offset(0);
        make.centerY.equalTo(solarImg);
    }];
    
    QMUILabel *inverterTitleLab = [QMUILabel labelWithFont:kBOLDSYSTEMFONT(12) textColor:kCOLOR_HEX_A(@"#171A1D", 0.6)];
    inverterTitleLab.text = @"Inverter";
    [self addSubview:inverterTitleLab];
    [inverterTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(weakSelf.inverterImg.mas_bottom).mas_offset(5);
        make.right.equalTo(weakSelf.inverterImg.mas_left).offset(5);
    }];
    
    _inverterLab = [QMUILabel labelWithFont:kSYSTEMFONT(valueFont) textColor:kCOLOR_HEX(@"#3F4443")];
    _inverterLab.text = @"--kW";
    [self addSubview:_inverterLab];
    [_inverterLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(inverterTitleLab.mas_top).offset(-5);
        make.centerX.equalTo(inverterTitleLab);
    }];
    
    self.inverterLab.hidden = YES;
    self.inverterVolLab.hidden = YES;
    
    
    
    
    
    //电网
    UIImageView *gridImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"kwt_home_topo_Grid"]];
    [self addSubview:gridImg];
    self.gridImg = gridImg;
    [gridImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(solarImg);
        make.centerY.equalTo(solarImg);
        make.right.equalTo(loadImg);
    }];
    
    QMUILabel *gridTitleLab = [QMUILabel labelWithFont:kBOLDSYSTEMFONT(12) textColor:kCOLOR_HEX_A(@"#171A1D", 0.6)];
    gridTitleLab.text = @"Grid";
    [self addSubview:gridTitleLab];
    self.gridTitleLab = gridTitleLab;
    [gridTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(gridImg.mas_bottom).offset(5);
        make.centerX.equalTo(gridImg);
    }];
    
    _gridLab = [QMUILabel labelWithFont:kSYSTEMFONT(valueFont) textColor:kCOLOR_HEX(@"#3F4443")];
    _gridLab.text = @"--kW";
    [self addSubview:_gridLab];
    [_gridLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(gridImg.mas_left).offset(-5);
        make.bottom.equalTo(gridImg.mas_centerY).offset(-5);
    }];
    
   
    
    //并机
    UIView *paralellView = [[UIView alloc]init];
//    paralellView.backgroundColor = kCOLOR_HEX(@"e0e0e0");
    [self addSubview:paralellView];
    self.paralellView = paralellView;
    [paralellView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.inverterImg.mas_bottom).offset(60);
        make.left.right.mas_offset(0);
        make.height.offset(120);
        make.bottom.mas_offset(0);
    }];
    
    _scrollView = [[UIScrollView alloc]init];
//    scrollView.backgroundColor = UIColor.yellowColor;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.delegate = self;
    [paralellView addSubview:_scrollView];
    [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
    }];
    _itemContainView = [[UIView alloc]init];
//    _itemContainView.backgroundColor = UIColor.blueColor;
    [_scrollView addSubview:_itemContainView];
    [_itemContainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_offset(0);
    }];
    NSMutableArray *arr = [NSMutableArray array];
    //最多8个并机
    for (int i = 0; i<8; i++) {
        RHDeviceTopoBatteryItemView *batView = [[RHDeviceTopoBatteryItemView alloc]init];
//        batView.backgroundColor = kRandomColor;
        [arr addObject:batView];
        [self.itemContainView addSubview:batView];
    }
    
    //上面的横线
    UIView *line_horizontal = kView;
    line_horizontal.backgroundColor = kCOLOR_HEX_A(@"#5AD8A6", 1);
    line_horizontal.tag = 400;
    [_itemContainView addSubview:line_horizontal];
    self.batViewArr = arr.copy;
    
    
    //------ 小圆点
    _solarDotView = [[UIView alloc] initWithFrame:CGRectMake(-200, 0, 6, 6)];
    [_solarDotView setBackgroundColor:kCOLOR_HEX_A(@"#F9C626", 1)];
    [_solarDotView.layer setCornerRadius:3];
    [self addSubview:_solarDotView];
    
    _gridDotView = [[UIView alloc] initWithFrame:CGRectMake(-200, 0, 6, 6)];
    [_gridDotView setBackgroundColor:kCOLOR_HEX_A(@"#42D5EA", 1)];
    [_gridDotView.layer setCornerRadius:3];
    [self addSubview:_gridDotView];
    
    _parallel_gridDotView = [[UIView alloc] initWithFrame:CGRectMake(-200, 0, 6, 6)];
    [_parallel_gridDotView setBackgroundColor:kCOLOR_HEX_A(@"#7073F0", 1)];
    [_parallel_gridDotView.layer setCornerRadius:3];
    [self addSubview:_parallel_gridDotView];
    
    _loadDotView = [[UIView alloc] initWithFrame:CGRectMake(-200, 0, 6, 6)];
    [_loadDotView setBackgroundColor:kCOLOR_HEX_A(@"#F9A4A3", 1)];
    [_loadDotView.layer setCornerRadius:3];
    [self addSubview:_loadDotView];
    
    _batteryDotView = [[UIView alloc] initWithFrame:CGRectMake(-200, 0, 6, 6)];
    [_batteryDotView setBackgroundColor:kCOLOR_HEX_A(@"#76D16E", 1)];
    [_batteryDotView.layer setCornerRadius:3];
    [self addSubview:_batteryDotView];

    _generatorDotView = [[UIView alloc] initWithFrame:CGRectMake(-200, 0, 6, 6)];
    [_generatorDotView setBackgroundColor:kCOLOR_HEX_A(@"#678EF2", 1)];
    [_generatorDotView.layer setCornerRadius:3];
    [self addSubview:_generatorDotView];

    BOOL isHEBAT = self.deviceModel.productTypeId == ProductType_HE_BAT;
    self.solarLab.hidden    = isHEBAT;
    self.solarVolLab.hidden = isHEBAT;
    
    self.generatorLab.hidden    = isHEBAT;
    self.generatorVolLab.hidden = isHEBAT;
    
    self.gridLab.hidden    = isHEBAT;
    self.gridVolLab.hidden = isHEBAT;
    
    self.loadLab.hidden    = isHEBAT;
    self.loadVolLab.hidden = isHEBAT;
}

-(UIView*)batViewWithIndex:(NSInteger)index
{
    UIView *batView = kView;
    UIView *line = kView;
    line.backgroundColor = kCOLOR_HEX(@"5AD8A6");
    [batView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(1);
        make.centerX.mas_offset(0);
        make.top.mas_offset(0);
        make.height.offset(20);
    }];
    
    UIImageView *borderImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"device_paralell_dashBorder"]];
    [batView addSubview:borderImage];
    [borderImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(@(CGSizeMake(54, 54)));
        make.centerX.mas_offset(0);
        make.top.equalTo(line.mas_bottom);
    }];
    
    QMUILabel *socLab = [QMUILabel labelWithFont:kSYSTEMFONT(8) textColor:kCOLOR_HEX(@"5AD8A6") textAligment:NSTextAlignmentCenter];
    socLab.text = @"--%";
    socLab.tag = 100 + index;
    [borderImage addSubview:socLab];
    [socLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_offset(0);
        make.top.mas_offset(5);
    }];
    
    UIImageView *batteryImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"device_paralell_batteryIcon"]];
    [borderImage addSubview:batteryImg];
    [batteryImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(@(CGSizeMake(19, 30)));
        make.centerX.mas_offset(0);
        make.bottom.mas_offset(-5);
    }];
    
    
//    QMUILabel *socTitleLab = [QMUILabel labelWithFont:kSYSTEMFONT(10) textColor:kCOLOR_HEX_A(@"#171A1D", 0.6) textAligment:NSTextAlignmentLeft];
//    socTitleLab.text = @"SOC";
//    [batView addSubview:socTitleLab];
//    [socTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(batteryImg).offset(5);
//        make.right.equalTo(batteryImg.mas_left).offset(- 5);
//        make.width.offset(40);
//    }];

    
    QMUILabel *batteryTitleLab = [QMUILabel labelWithFont:kSYSTEMFONT(10) textColor:kCOLOR_HEX_A(@"#171A1D", 0.6) textAligment:NSTextAlignmentLeft];
    batteryTitleLab.text = @"Power";
    [batView addSubview:batteryTitleLab];
    [batteryTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(borderImage.mas_bottom).offset(5);
        make.left.equalTo(borderImage);
    }];
    
    QMUILabel *batteryPowerLab = [QMUILabel labelWithFont:kSYSTEMFONT(12) textColor:kCOLOR_HEX(@"#3F4443") textAligment:NSTextAlignmentLeft];
    batteryPowerLab.text = @"--W";
    batteryPowerLab.tag = 200 + index;
    [batView addSubview:batteryPowerLab];
    [batteryPowerLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(batteryTitleLab);
        make.top.equalTo(batteryTitleLab.mas_bottom).offset(5);
        make.bottom.mas_offset(-5);
    }];

    
    
//    QMUILabel *deviceIdLab = [QMUILabel labelWithFont:kSYSTEMFONT(12) textColor:kCOLOR_HEX(@"#3F4443") textAligment:NSTextAlignmentCenter];
//    deviceIdLab.text = @"--";
//    deviceIdLab.tag = 300+index;
//    [batView addSubview:deviceIdLab];
//    [deviceIdLab mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(batteryImg.mas_bottom).offset(8);
//        make.left.right.mas_offset(0);
//        make.bottom.mas_offset(0);
//    }];
    return batView;
}

- (void)drawRect:(CGRect)rect
{
    //-------- 连线
    UIBezierPath *solar_inver = [UIBezierPath bezierPath];
    solar_inver.lineWidth = line_Width;
   
    [kCOLOR_HEX(@"#F9C626") setStroke];
    [solar_inver moveToPoint:CGPointMake(self.solarImg.right, self.solarImg.centerY)];
    [solar_inver addLineToPoint:CGPointMake(self.inverterImg.left , self.solarImg.centerY)];
    [solar_inver stroke];
    self.solarPath = solar_inver;
 
    //-------- 连线
    UIBezierPath *inverter_grid = [UIBezierPath bezierPath];
    inverter_grid.lineWidth = line_Width;
   
    [kCOLOR_HEX(@"#5AD8A6")  setStroke];
    [inverter_grid moveToPoint:CGPointMake(self.inverterImg.right, self.inverterImg.centerY)];
    [inverter_grid addLineToPoint:CGPointMake(self.gridImg.left , self.gridImg.centerY)];
    [inverter_grid stroke];
    self.gridPath = inverter_grid;
    
    
    
    
    
    //-------- 连线
    UIBezierPath *generator_inverter = [UIBezierPath bezierPath];
    generator_inverter.lineWidth = line_Width;
    [kCOLOR_HEX(@"#678EF2") setStroke];
    [generator_inverter moveToPoint:CGPointMake(self.generatorImg.right, self.generatorImg.centerY)];
    [generator_inverter addLineToPoint:CGPointMake(self.inverterImg.centerX-5-5 , self.generatorImg.centerY)];
    UIBezierPath *circle_generator = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.inverterImg.centerX-5-5, self.generatorImg.centerY+5) radius:5 startAngle:M_PI+M_PI_2 endAngle:M_PI*2 clockwise:YES];
    [generator_inverter appendPath:circle_generator];
    [generator_inverter addLineToPoint:CGPointMake(self.inverterImg.centerX-5 , self.inverterImg.top)];
    [generator_inverter stroke];
    self.generationPath = generator_inverter;
    
    //-------- 连线
    UIBezierPath *inverter_battery = [UIBezierPath bezierPath];
    inverter_battery.lineWidth = line_Width;
    [kCOLOR_HEX(@"#5AD8A6") setStroke];
    [inverter_battery moveToPoint:CGPointMake(self.inverterImg.centerX, self.inverterImg.bottom )];
    [inverter_battery addLineToPoint:CGPointMake(self.paralellView.centerX, self.paralellView.top)];
    [inverter_battery stroke];
    self.batteryPath = inverter_battery;
    
    //-------- 连线
    UIBezierPath *inverter_load = [UIBezierPath bezierPath];
    inverter_load.lineWidth = line_Width;
    [kCOLOR_HEX(@"#42D5EA") setStroke];
    [inverter_load moveToPoint:CGPointMake(self.loadImg.left ,self.loadImg.centerY)];
    [inverter_load addLineToPoint:CGPointMake(self.inverterImg.centerX+5+5 ,self.loadImg.centerY)];
    UIBezierPath *circle_load = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.inverterImg.centerX+5+5, self.loadImg.centerY+5) radius:5 startAngle:M_PI+M_PI_2 endAngle:M_PI clockwise:NO];
    [inverter_load appendPath:circle_load];
    [inverter_load addLineToPoint:CGPointMake(self.inverterImg.centerX+5 ,self.inverterImg.top)];
    [inverter_load stroke];
    self.loadPath = [inverter_load bezierPathByReversingPath];
    
    //有时设置数据源时 路径还没生成
   
    [self updateMicroGridAnimation];
    
}

//设备进来  //只有电池才除以1000
-(void)setTopoModel:(RHTopoModel *)topoModel deviceModel:(nonnull RHDeviceModel *)deviceModel
{
    kWeakSelf;
    [self setNeedsDisplay];
    self.topoModel = topoModel;
    self.deviceModel = deviceModel;
    
    //测试
//    self.deviceModel.status = @"0";
//    self.topoModel.pvPwr = @"1";
//    self.topoModel.genPwr = @"2";
//    self.topoModel.loadPwr = @"3";
//    self.topoModel.gridPwr = @"4";
//    self.topoModel.batPwr = @"5";
    
    //设备在线但逆变器离线 图标置灰
    BOOL invState = (self.deviceModel.invState == 0&&[self.deviceModel.status isEqualToString:@"0"]);
    
    NSString *InvImageStr = invState?@"kwt_home_topo_Inv_gray":@"kwt_home_topo_inverter";
    self.inverterImg.image = [UIImage imageNamed:InvImageStr];
    
    //-------------------------------------------------------------------------
    
    self.solarLab.text        = @"--kW";
    self.gridLab.text         = @"--kW";
    self.loadLab.text         = @"--kW";
    self.generatorLab.text    = @"--kW";
    self.batteryPowerLab.text = @"--kW";
    self.parallelLab.text = @"--kW";
    
   
    BOOL isOnline = [self.deviceModel.status isEqualToString:@"0"];
    
    
    [self updateBatView];
    [self setNeedsDisplay];
    //设备离线
   
    if(!isOnline) return;
   
    
    //电池功率
    NSDecimalNumber *batteryPower = [NSDecimalNumber decimalNumberWithDouble:[topoModel.batPwr doubleValue]];
    self.batteryPowerLab.text = [NSString stringWithFormat:@"%@kW",batteryPower];
    
    //soc
    NSString *socStr = [NSString stringWithFormat:@"%@%%",[NSDecimalNumber decimalPlacesWithRoundUpWithDigit:3 value:topoModel.soc]];
    self.socLab.text = [NSString stringWithFormat:@"%@",socStr];
    
    //逆变器离线只显示电池数据
    if (self.deviceModel.invState == 0) {
        return;
    }
    
    //光伏功率
    NSString *solarStr = [NSString stringWithFormat:@"%@ kW",[NSDecimalNumber decimalPlacesWithRoundUpWithDigit:3 value:[topoModel.pvPwr doubleValue]]];
    self.solarLab.attributedText = [NSString attributeStringWithContent:solarStr attriFont:kSYSTEMFONT(8) attriColor:kCOLOR_HEX_A(@"#3F4443", 0.6) attriKey:@"kW"];
    
    
    //电网功率
    NSString *gridStr = [NSString stringWithFormat:@"%@ kW",[NSDecimalNumber decimalPlacesWithRoundUpWithDigit:3 value:[topoModel.gridPwr doubleValue]]];
    self.gridLab.attributedText = [NSString attributeStringWithContent:gridStr attriFont:kSYSTEMFONT(8) attriColor:kCOLOR_HEX_A(@"#3F4443", 0.6) attriKey:@"kW"];
    
    
    //负载功率  (离网负载功率)
    NSString *loadStr = [NSString stringWithFormat:@"%@ kW",[NSDecimalNumber decimalPlacesWithRoundUpWithDigit:3 value:[topoModel.loadPwr doubleValue]]];
    self.loadLab.attributedText = [NSString attributeStringWithContent:loadStr attriFont:kSYSTEMFONT(8) attriColor:kCOLOR_HEX_A(@"#3F4443", 0.6) attriKey:@"kW"];
    
    //发电机功率
    NSString *generatorStr = [NSString stringWithFormat:@"%@ kW",[NSDecimalNumber decimalPlacesWithRoundUpWithDigit:3 value:[topoModel.genPwr doubleValue]]];
    self.generatorLab.attributedText = [NSString attributeStringWithContent:generatorStr attriFont:kSYSTEMFONT(8) attriColor:kCOLOR_HEX_A(@"#3F4443", 0.6) attriKey:@"kW"];
    
    //麦格瑞能并网功率
    NSString *parallelStr = [NSString stringWithFormat:@"%@ kW",[NSDecimalNumber decimalPlacesWithRoundUpWithDigit:3 value:[topoModel.extLoadPower doubleValue]]];
    self.parallelLab.attributedText = [NSString attributeStringWithContent:parallelStr attriFont:kSYSTEMFONT(8) attriColor:kCOLOR_HEX_A(@"#3F4443", 0.6) attriKey:@"kW"];
}

#pragma mark -------- 微网的动画和户用的方向相反
-(void)updateMicroGridAnimation
{
    if(![self.deviceModel.status isEqualToString:@"0"]) return;
    //电池动画  电池正值  电池流向逆变器  电池负值    逆变器流向电池
    if([_topoModel.batPwr floatValue]<-1){
        self.batteryDotView.hidden = NO;
        [self addAnimationWithPointView:self.batteryDotView path:self.batteryPath duration:3 animationName:@"battery"];
    }else if ([_topoModel.batPwr floatValue] > 1){
        self.batteryDotView.hidden = NO;
        [self addAnimationWithPointView:self.batteryDotView path:self.batteryPath.bezierPathByReversingPath duration:3 animationName:@"battery_rever"];
    }else{
        [self.batteryDotView.layer removeAllAnimations];
    }
    
    //光伏动画
    if([_topoModel.pvPwr floatValue]>1){
        self.solarDotView.hidden = NO;
        [self addAnimationWithPointView:self.solarDotView path:self.solarPath duration:3 animationName:@"solar"];
    }else{
        [self.solarDotView.layer removeAllAnimations];
    }
    
   //电网动画  正(买电):grid---->inverter    负(卖电):inverter -----> grid
    if([_topoModel.gridPwr floatValue]>1){
        self.gridDotView.hidden = NO;
        [self addAnimationWithPointView:self.gridDotView path:self.gridPath duration:3 animationName:@"grid"];
    }else if ([_topoModel.gridPwr floatValue]< -1){
        self.gridDotView.hidden = NO;
        [self addAnimationWithPointView:self.gridDotView path:self.gridPath.bezierPathByReversingPath duration:3 animationName:@"grid_rever"];
    }else{
        [self.gridDotView.layer removeAllAnimations];
    }
    
    //负载动画
    if([_topoModel.loadPwr floatValue]>1){
        self.loadDotView.hidden = NO;
        [self addAnimationWithPointView:self.loadDotView path:self.loadPath duration:3 animationName:@"load"];
    }else{
        [self.loadDotView.layer removeAllAnimations];
    }
    
    //发电机动画
    if([_topoModel.genPwr floatValue]>1){
        self.generatorDotView.hidden = NO;
        [self addAnimationWithPointView:self.generatorDotView path:self.generationPath duration:3 animationName:@"genrator"];
    }else{
        [self.generatorDotView.layer removeAllAnimations];
    }
}

//更新并机数量
-(void)updateBatView
{
   
    [self.batViewArr enumerateObjectsUsingBlock:^(UIView*  _Nonnull itemView, NSUInteger idx, BOOL * _Nonnull stop) {
            [itemView removeFromSuperview];
    }];
    //每页放4个
    CGFloat itemWidth = ( MIN(kUI_WINDOW_W, kUI_WINDOW_H)  - 30 - 20 )/4;
    __block UIView *lastView;
  
    NSInteger deviceCount = self.topoModel.batteryList.count>0?self.topoModel.batteryList.count:1;
    for (int i = 0; i<deviceCount; i++) {
        RHDeviceTopoBatteryItemView *batView = [self.batViewArr safeObjectAtIndex:i];
    
        NSDictionary *dic = [self.topoModel.batteryList safeObjectAtIndex:i];
        
        [batView setDataDic:dic mainDeviceId:self.deviceModel.deviceId status:[self.deviceModel.status integerValue]==0];
        
        [self.itemContainView addSubview:batView];
        if (lastView) {
            [batView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(lastView.mas_right).offset(0);
                make.size.equalTo(lastView);
                make.top.bottom.mas_offset(0);
            }];
        }else{
            [batView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_offset(0);
                make.width.offset(itemWidth);
                make.top.bottom.mas_offset(0);
            }];
        }
        lastView = batView;
    }
    UIView *firstItem = [self.batViewArr firstObject];
    UIView *lastItem = [self.batViewArr safeObjectAtIndex:self.topoModel.batteryList.count-1];
    if (lastItem == nil) {
        lastItem = firstItem;
    }
    
    UIView *line_horizontal = [self.itemContainView viewWithTag:400];
    [line_horizontal mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(0);
        make.left.equalTo(firstItem.mas_centerX);
        make.right.equalTo(lastItem.mas_centerX);
        make.height.offset(1);
    }];
    [self.itemContainView bringSubviewToFront:line_horizontal];
    [lastView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(0);
    }];
    
    //不足4个 或者是全屏 居中排列
    if (self.topoModel.batteryList.count<4 || self.isLandscape) {
        [self.itemContainView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.mas_offset(0);
            make.centerX.mas_offset(0);
        }];
    }else{
        [self.itemContainView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.edges.insets(UIEdgeInsetsZero);
        }];
    }
}




-(void)addAnimationWithPointView:(UIView*)pointView path:(UIBezierPath*)path duration:(NSTimeInterval)duration animationName:(NSString*)name
{
    [pointView.layer removeAllAnimations];
    CAKeyframeAnimation *pointAnimation = [self createAnimationWithPath:path duration:duration];
    [pointView.layer addAnimation:pointAnimation forKey:name];
}

-(CAKeyframeAnimation*)createAnimationWithPath:(UIBezierPath*)path duration:(NSTimeInterval)duration
{
    CAKeyframeAnimation *keyFrameAni = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    keyFrameAni.path = path.CGPath;
    keyFrameAni.removedOnCompletion = NO;
    keyFrameAni.duration = duration;
    keyFrameAni.repeatCount = MAXFLOAT;
    keyFrameAni.calculationMode = kCAAnimationPaced;;
    
    return keyFrameAni;
}

@end
